package com.htc.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.htc.model.InternalResponse;
import com.htc.model.Banner;
import com.htc.model.request.BannerRequest;
import com.htc.service.BannerService;

import com.htc.error.InternalError;

@RestController
@RequestMapping("/banner")
public class BannerController {
	@Autowired
	private BannerService bannerService;

	@RequestMapping("queryList")
	public InternalResponse queryList(BannerRequest request){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(bannerService.queryList(request));
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}
	
	
	@RequestMapping("findAll")
	public InternalResponse findAll(){
		InternalResponse internalResponse = null;
	    try{
	    	internalResponse = InternalResponse.success().withBody(bannerService.findAll());
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}

	@RequestMapping("save")
	public InternalResponse save(Banner banner){
		InternalResponse internalResponse = null;
	    try{
	    	if(banner.getId()==null) {
	    		bannerService.add(banner);
	    	}else {
	    		bannerService.update(banner);
	    	}
	    	internalResponse = InternalResponse.success();
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}

	@RequestMapping("del")
	public InternalResponse del(int id){
		InternalResponse internalResponse = null;
	    try{
	    	bannerService.del(id);
	    	internalResponse = InternalResponse.success();
	    }catch(InternalError e){
            internalResponse = InternalResponse.fail().withBody(e.getErrorMsg());
	    }catch(Exception e){
            e.printStackTrace();
            internalResponse = InternalResponse.fail();
	    }
	    return internalResponse;
	}


}
